package roster;

public class ScheduleModifier {
	public static Schedule result=null;
	/*eg:
	 *	change date 16032013
	 *	change 1 7 AB
	 *	add Peter
	 *	delete 1
	 *
	**/
	public static boolean query(Schedule obj, String query){
		result=obj;
		String[] command=query.split(" ");
		if(command.length==0)return false;
		//change command
		if(command[0].equals("change")){
			if(command.length==3){
				if(!command[1].equals("date"))return false;
				try{
					obj.setStartDate(Integer.parseInt(command[2]));
				}catch(NumberFormatException e){return false;}
				
			}else if(command.length==4){
				try{ 
					int i=Integer.parseInt(command[1]);
					int j=Integer.parseInt(command[2]);
					if(i<1 || i>obj.getNoOfRecords() || j<1 || j>Schedule.noOfDays)
						return false;
					obj.setTime(i-1, j-1, command[3]);
				}catch(NumberFormatException e){return false;}
				
			}else
				return false;
			
		//add command	
		}else if(command[0].equals("add")){
			if(command.length==2){
				obj.addStaff(command[1]);
			}else 
				return false;
	
		//delete command
		}else if(command[0].equals("delete")){
			if(command.length==2){
				try{
					int tmp=Integer.parseInt(command[1]);
					if(tmp<1 || tmp>obj.getNoOfRecords())
						return false;
					obj.deleteRecord(tmp-1);
				}catch(NumberFormatException e){return false;}
			}else 
				return false;
			
		//other unsupported command
		}else
			return false;
		
		result=obj;
		return true;
	}

	public static Schedule getResult(){
		return result;
	}
}